package com.example.anjia03_3z.controller;


import com.example.anjia03_3z.pojo.dto.UserAddNewDTO;
import com.example.anjia03_3z.pojo.dto.UserUpdateDTO;
import com.example.anjia03_3z.pojo.vo.UserListItemVO;
import com.example.anjia03_3z.pojo.vo.UserStandardVO;
import com.example.anjia03_3z.service.IUserService;
import com.example.anjia03_3z.web.JsonPage;
import com.example.anjia03_3z.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


@RestController
@RequestMapping("/users")
@Api(tags = "01.用户管理")
@Validated
public class UserController {

    @Autowired
    IUserService service;

    @PostMapping("/add-new")
    @ApiOperation("添加用户")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid UserAddNewDTO userAddNewDTO){
        service.addNew(userAddNewDTO);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}/delete")
    @ApiOperation("删除用户")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "相册编号", dataType = "Long", required = true)
    })
    public JsonResult<Void> delete(
            @Range(min = 1, message = "数据范围异常")
            @PathVariable
                    Long id) {
        service.deleteById(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
//    多个不冲突的正则表达式可以共存 有占位符与无占位符可以共存 无占位符但满足正则表达式会优先使用较为精准的方法
    @ApiOperation("修改相册")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "相册编号", dataType = "Long", required = true)
    })
    public JsonResult<Void> update(
            @Range(min = 1, message = "数据范围异常")
            @PathVariable
                    Long id,
            @Valid
                    UserUpdateDTO userUpdateDTO) {
        service.updateById(id, userUpdateDTO);
        return JsonResult.ok();
    }


    @GetMapping("/{page}/list")
    @ApiOperation("查询用户列表")
    @ApiOperationSupport(order = 402)
    public JsonResult<JsonPage<UserListItemVO>> listByParentId(
            @PathVariable
                    Integer page
    ){
        JsonPage<UserListItemVO> list = service.list(page, 5);
        return JsonResult.ok(list);
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("查询用户")
    @ApiOperationSupport(order = 403)
    public JsonResult<UserStandardVO> getStandard(
            @Range(min = 0, message = "数据范围异常")
            @PathVariable
                    Long id){
        UserStandardVO standard = service.getStandard(id);
        return JsonResult.ok(standard);
    }




}
